package com.h3bpm.kumhosunny.common.utils;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import com.h3bpm.base.util.SqlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author zhengyj
 * @date 2019/9/17
 * @description 常用的方法
 */
public class NormalUtil extends ControllerBase {

    private static final Logger logger = LoggerFactory.getLogger(SqlUtil.class);

    @Override
    public String getFunctionCode() {
        return null;
    }


    public static String getInstanceNameById(String instanceId) throws Exception {
        InstanceData instanceData = null;
        try {
            instanceData = new InstanceData(getEngine(), instanceId, User.AdministratorID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instanceData.getInstanceContext().getInstanceName();
    }

    public static InstanceData getInstanceDataById(String instanceId) throws Exception {
        InstanceData instanceData = null;
        try {
            instanceData = new InstanceData(getEngine(), instanceId, User.AdministratorID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instanceData;
    }

    /***
     * 功能描述: 根据流程id获取最后一条审批意见
     *
     * @Param: [instanceId]
     * @Return: java.lang.String
     * @Author: zhengyj
     * @Date: 2019/7/6 11:41
     */
    public static  String getLastCommonByInstanceId(String instanceId) {
        String common = "";
        String querySql = "SELECT * from (  SELECT TEXT from OT_COMMENT WHERE INSTANCEID = '%s' ORDER BY CREATEDTIME desc ) where rownum= 1";
        try {
            common = SqlUtil.doQuery(String.format(querySql,instanceId)).getRows().get(0).getString(0);
        } catch (Exception e) {
            logger.error("=======getLastCommonByInstanceId===="+e.getMessage());
        }
        return common;
    }

    /****
     * 功能描述: 根据 bizobjectid和附件字段名 获取附件
     *
     * @Param: [bizObjectId, fieldName]
     * @Return: java.util.List<java.lang.Object>
     * @Author: zhengyj
     * @Date: 2019/9/17 14:17
     */
    public static List<Object> getAttachments(String bizObjectId, String fieldName) throws Exception {
        //查询附件sql
        String attachmentSql = "SELECT OBJECTID as AttachmentId,BIZOBJECTID,CONTENTLENGTH,CONTENTTYPE,FILENAME,BIZOBJECTSCHEMACODE,DATAFIELD FROM OT_ATTACHMENT WHERE BIZOBJECTID = '%s' and DATAFIELD = '%s' ";


        return DateTableUtil.getRowList(SqlUtil.doQuery(String.format(attachmentSql,bizObjectId,fieldName)));

    }

}
